// ==UserScript==
// @name         自动刷课
// @namespace    http://tampermonkey.net/
// @version      2024-10-25
// @description  自动刷课
// @author       me
// @match        https://*.ouchn.cn/course/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    
    function delayAndRun(func, time) {
        setTimeout(func, time * 1000)
    }

    // 获取当前菜单
    function selectCurMenu() {

        function getTitle(elem, type) {

            return elem.querySelector(`.full-screen-mode-sidebar-${type}`).innerText
        }

        const menu_path = []
        const menu_path_title = []
        
        const current_menu_item = document.querySelector('.full-screen-mode-sidebar-menu-item.active')
        const current_menu_item_title = getTitle(current_menu_item, 'menu-item-title')
        const sub_menu = current_menu_item.parentElement.parentElement
        const sub_menu_title = getTitle(sub_menu, 'sub-menu-title')
        
        menu_path.push(current_menu_item, sub_menu)
        menu_path_title.push(current_menu_item_title, sub_menu_title)

        if (sub_menu.parentElement.className != 'full-screen-mode-sidebar-menu') {
            const top_menu = sub_menu.parentElement.parentElement
            const top_menu_title = getTitle(top_menu, 'sub-menu-title')
            menu_path.push(top_menu)
            menu_path_title.push(top_menu_title)
        }

        console.debug('menu_path:')
        console.debug(menu_path_title)
        console.debug(menu_path)
        
        return menu_path
    }

    function isMenuItem(menuElement) {
        return menuElement.className.split(' ').includes('full-screen-mode-sidebar-menu-item')
    }

    function checkSubMenuInner(element) {
        let inner = element.querySelector('.full-screen-mode-sidebar-sub-menu-inner')

        return inner ? true : false
    }

    function clickSubMenuTitle(element) {
        const next_title = element.querySelector('.full-screen-mode-sidebar-sub-menu-title')
        console.debug('try to click next:')
        console.debug(next_title)

        next_title.click()
    }

    function getFirstChildMenu(menuElement) {

        let next_menu = menuElement.querySelector('.full-screen-mode-sidebar-sub-menu')
        
        if (!next_menu) {
            next_menu = menuElement.querySelector('.full-screen-mode-sidebar-menu-item')
        }

        return next_menu
    }

    

    function clickNextLevel(menuElement, delay, callback) {
        clickSubMenuTitle(menuElement)

        
        delayAndRun(()=>{

            let next_menu = getFirstChildMenu(menuElement)
            
            console.debug('next_menu:')
            console.debug(next_menu)

            clickMenu(next_menu, callback)
        }, delay)
    }


    function clickMenu(menuElement, callback) {

        

        if (isMenuItem(menuElement)) {
            menuElement.click()
            callback(true)
        } else {

            if (checkSubMenuInner(menuElement)) {
                //立刻打开下一级菜单并点击

                let first_child_menu = getFirstChildMenu(menuElement)

                if (first_child_menu) {
                    if (isMenuItem(first_child_menu)) {
                        clickMenu(first_child_menu, callback)
                    } else {
                        clickNextLevel(first_child_menu, 1, callback)
                    }
                } else {
                    callback(false)
                }
            } else {
                // 点击下一级菜单后等5秒再点击
                clickNextLevel(menuElement, 5, callback)
            }
            
        }
    }


    function nextMenu(menu_path, callback) {
        
        

        let next = menu_path[0].nextElementSibling

        if (next) {
            // 有下一级菜单
            clickMenu(next, callback)
        } else {
            // 没有下一级菜单，返回上一级菜单

            if (menu_path.length == 1) {
                console.debug('end of menu?')
                callback(false)
            } else {
                nextMenu(menu_path.slice(1), callback)    
            }
        }
        
    }
    


    function goNextMenu() {
        // 获取当前菜单
        const menu_path = selectCurMenu()
        
        console.debug('menu_path: ')
        console.debug(menu_path)
        
        nextMenu(menu_path, (result)=>{
            console.debug('next menu result: ', result)

            if (result) {
                delayAndRun(watchVideo, 5)
            }
        })

    }


    function playVideo(video) {
        

        // 判断当前标签的视频状态

        const ended = video.ended

        if (ended) {
            return
        }

        video.playbackRate = 2.0

        // 播放视频
        const play_button = document.querySelector('.mvp-toggle-play')

        if (play_button.querySelector('i.mvp-fonts-play')) {
            play_button.click()
        }
    }


    function getCurrentVideo() {
        const video = document.querySelector('video')

        return video
    }


    function listenVideoEnd(video) {
        function onVideoEnded() {

            console.debug('video ended')

            goNextMenu()

            video.removeEventListener('ended', onVideoEnded)
        }

        video.addEventListener('ended', onVideoEnded)
    }

    // 看视频
    function watchVideo() {

        const video = getCurrentVideo()

        if (video) {

            if (video.ended) {

                goNextMenu()
                
            } else {
                playVideo(video)
        
                listenVideoEnd(video)
            }
            
            
        } else {
            goNextMenu()
        }
        
    }


    function main() {

        if (window.auto_take_course) {
            return
        }

        window.auto_take_course = true

        const menu = document.querySelector('.full-screen-mode-sidebar-menu')
        
        if (!menu) {
            return
        }

        const video = getCurrentVideo()

        if (video) {
            
            delayAndRun(()=>{
                listenVideoEnd(video)
            }, 5)
        } else {
            goNextMenu()
        }

        
    }

    

    delayAndRun(main, 5)

    console.log('自动刷课')
})();